package com.java110.mall.shop.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.shop.ShopReturnAddressDto;
import com.java110.po.shop.ShopReturnAddressPo;
import com.java110.mall.shop.bmo.shopReturnAddress.IDeleteShopReturnAddressBMO;
import com.java110.mall.shop.bmo.shopReturnAddress.IGetShopReturnAddressBMO;
import com.java110.mall.shop.bmo.shopReturnAddress.ISaveShopReturnAddressBMO;
import com.java110.mall.shop.bmo.shopReturnAddress.IUpdateShopReturnAddressBMO;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/shopReturnAddress")
public class ShopReturnAddressApi {

    @Autowired
    private ISaveShopReturnAddressBMO saveShopReturnAddressBMOImpl;
    @Autowired
    private IUpdateShopReturnAddressBMO updateShopReturnAddressBMOImpl;
    @Autowired
    private IDeleteShopReturnAddressBMO deleteShopReturnAddressBMOImpl;

    @Autowired
    private IGetShopReturnAddressBMO getShopReturnAddressBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopReturnAddress/saveShopReturnAddress
     * @path /app/shopReturnAddress/saveShopReturnAddress
     */
    @RequestMapping(value = "/saveShopReturnAddress", method = RequestMethod.POST)
    public ResponseEntity<String> saveShopReturnAddress(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "name", "请求报文中未包含name");
        Assert.hasKeyAndValue(reqJson, "link", "请求报文中未包含link");
        Assert.hasKeyAndValue(reqJson, "address", "请求报文中未包含address");
        Assert.hasKeyAndValue(reqJson, "seq", "请求报文中未包含seq");


        ShopReturnAddressPo shopReturnAddressPo = BeanConvertUtil.covertBean(reqJson, ShopReturnAddressPo.class);
        shopReturnAddressPo.setState("T");
        return saveShopReturnAddressBMOImpl.save(shopReturnAddressPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopReturnAddress/updateShopReturnAddress
     * @path /app/shopReturnAddress/updateShopReturnAddress
     */
    @RequestMapping(value = "/updateShopReturnAddress", method = RequestMethod.POST)
    public ResponseEntity<String> updateShopReturnAddress(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含shopId");
        Assert.hasKeyAndValue(reqJson, "name", "请求报文中未包含name");
        Assert.hasKeyAndValue(reqJson, "link", "请求报文中未包含link");
        Assert.hasKeyAndValue(reqJson, "address", "请求报文中未包含address");
        Assert.hasKeyAndValue(reqJson, "seq", "请求报文中未包含seq");
        Assert.hasKeyAndValue(reqJson, "addressId", "addressId不能为空");


        ShopReturnAddressPo shopReturnAddressPo = BeanConvertUtil.covertBean(reqJson, ShopReturnAddressPo.class);
        return updateShopReturnAddressBMOImpl.update(shopReturnAddressPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /shopReturnAddress/deleteShopReturnAddress
     * @path /app/shopReturnAddress/deleteShopReturnAddress
     */
    @RequestMapping(value = "/deleteShopReturnAddress", method = RequestMethod.POST)
    public ResponseEntity<String> deleteShopReturnAddress(@RequestBody JSONObject reqJson) {
//        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");
        Assert.hasKeyAndValue(reqJson, "addressId", "addressId不能为空");
        ShopReturnAddressPo shopReturnAddressPo = BeanConvertUtil.covertBean(reqJson, ShopReturnAddressPo.class);
        return deleteShopReturnAddressBMOImpl.delete(shopReturnAddressPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param shopId 小区ID
     * @return
     * @serviceCode /shopReturnAddress/queryShopReturnAddress
     * @path /app/shopReturnAddress/queryShopReturnAddress
     */
    @RequestMapping(value = "/queryShopReturnAddress", method = RequestMethod.GET)
    public ResponseEntity<String> queryShopReturnAddress(@RequestParam(value = "shopId") String shopId,
                                                         @RequestParam(value = "page") int page,
                                                         @RequestParam(value = "row") int row) {
        ShopReturnAddressDto shopReturnAddressDto = new ShopReturnAddressDto();
        shopReturnAddressDto.setPage(page);
        shopReturnAddressDto.setRow(row);
        shopReturnAddressDto.setShopId(shopId);
        return getShopReturnAddressBMOImpl.get(shopReturnAddressDto);
    }
}
